using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace BookShop
{
	/// <summary>
	/// Strongly-typed collection for the Book class.
	/// </summary>
    [Serializable]
	public partial class BookCollection : ActiveList<Book, BookCollection>
	{	   
		public BookCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>BookCollection</returns>
		public BookCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                Book o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the Book table.
	/// </summary>
	[Serializable]
	public partial class Book : ActiveRecord<Book>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Book()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Book(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Book(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Book(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Book", TableType.Table, DataService.GetInstance("BookShop"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarIsbn = new TableSchema.TableColumn(schema);
				colvarIsbn.ColumnName = "ISBN";
				colvarIsbn.DataType = DbType.AnsiStringFixedLength;
				colvarIsbn.MaxLength = 20;
				colvarIsbn.AutoIncrement = false;
				colvarIsbn.IsNullable = false;
				colvarIsbn.IsPrimaryKey = true;
				colvarIsbn.IsForeignKey = false;
				colvarIsbn.IsReadOnly = false;
				colvarIsbn.DefaultSetting = @"";
				colvarIsbn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsbn);
				
				TableSchema.TableColumn colvarBookName = new TableSchema.TableColumn(schema);
				colvarBookName.ColumnName = "BookName";
				colvarBookName.DataType = DbType.AnsiString;
				colvarBookName.MaxLength = 50;
				colvarBookName.AutoIncrement = false;
				colvarBookName.IsNullable = false;
				colvarBookName.IsPrimaryKey = false;
				colvarBookName.IsForeignKey = false;
				colvarBookName.IsReadOnly = false;
				colvarBookName.DefaultSetting = @"";
				colvarBookName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarBookName);
				
				TableSchema.TableColumn colvarAuthor = new TableSchema.TableColumn(schema);
				colvarAuthor.ColumnName = "Author";
				colvarAuthor.DataType = DbType.AnsiString;
				colvarAuthor.MaxLength = 50;
				colvarAuthor.AutoIncrement = false;
				colvarAuthor.IsNullable = false;
				colvarAuthor.IsPrimaryKey = false;
				colvarAuthor.IsForeignKey = false;
				colvarAuthor.IsReadOnly = false;
				colvarAuthor.DefaultSetting = @"";
				colvarAuthor.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAuthor);
				
				TableSchema.TableColumn colvarPublisher = new TableSchema.TableColumn(schema);
				colvarPublisher.ColumnName = "Publisher";
				colvarPublisher.DataType = DbType.AnsiString;
				colvarPublisher.MaxLength = 50;
				colvarPublisher.AutoIncrement = false;
				colvarPublisher.IsNullable = false;
				colvarPublisher.IsPrimaryKey = false;
				colvarPublisher.IsForeignKey = false;
				colvarPublisher.IsReadOnly = false;
				colvarPublisher.DefaultSetting = @"";
				colvarPublisher.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPublisher);
				
				TableSchema.TableColumn colvarPrice = new TableSchema.TableColumn(schema);
				colvarPrice.ColumnName = "Price";
				colvarPrice.DataType = DbType.Currency;
				colvarPrice.MaxLength = 0;
				colvarPrice.AutoIncrement = false;
				colvarPrice.IsNullable = false;
				colvarPrice.IsPrimaryKey = false;
				colvarPrice.IsForeignKey = false;
				colvarPrice.IsReadOnly = false;
				colvarPrice.DefaultSetting = @"";
				colvarPrice.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPrice);
				
				TableSchema.TableColumn colvarCategoryID = new TableSchema.TableColumn(schema);
				colvarCategoryID.ColumnName = "CategoryID";
				colvarCategoryID.DataType = DbType.Int32;
				colvarCategoryID.MaxLength = 0;
				colvarCategoryID.AutoIncrement = false;
				colvarCategoryID.IsNullable = true;
				colvarCategoryID.IsPrimaryKey = false;
				colvarCategoryID.IsForeignKey = true;
				colvarCategoryID.IsReadOnly = false;
				colvarCategoryID.DefaultSetting = @"";
				
					colvarCategoryID.ForeignKeyTableName = "Category";
				schema.Columns.Add(colvarCategoryID);
				
				TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
				colvarDescription.ColumnName = "Description";
				colvarDescription.DataType = DbType.AnsiString;
				colvarDescription.MaxLength = 1000;
				colvarDescription.AutoIncrement = false;
				colvarDescription.IsNullable = true;
				colvarDescription.IsPrimaryKey = false;
				colvarDescription.IsForeignKey = false;
				colvarDescription.IsReadOnly = false;
				colvarDescription.DefaultSetting = @"";
				colvarDescription.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDescription);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["BookShop"].AddSchema("Book",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("Isbn")]
		[Bindable(true)]
		public string Isbn 
		{
			get { return GetColumnValue<string>(Columns.Isbn); }
			set { SetColumnValue(Columns.Isbn, value); }
		}
		  
		[XmlAttribute("BookName")]
		[Bindable(true)]
		public string BookName 
		{
			get { return GetColumnValue<string>(Columns.BookName); }
			set { SetColumnValue(Columns.BookName, value); }
		}
		  
		[XmlAttribute("Author")]
		[Bindable(true)]
		public string Author 
		{
			get { return GetColumnValue<string>(Columns.Author); }
			set { SetColumnValue(Columns.Author, value); }
		}
		  
		[XmlAttribute("Publisher")]
		[Bindable(true)]
		public string Publisher 
		{
			get { return GetColumnValue<string>(Columns.Publisher); }
			set { SetColumnValue(Columns.Publisher, value); }
		}
		  
		[XmlAttribute("Price")]
		[Bindable(true)]
		public decimal Price 
		{
			get { return GetColumnValue<decimal>(Columns.Price); }
			set { SetColumnValue(Columns.Price, value); }
		}
		  
		[XmlAttribute("CategoryID")]
		[Bindable(true)]
		public int? CategoryID 
		{
			get { return GetColumnValue<int?>(Columns.CategoryID); }
			set { SetColumnValue(Columns.CategoryID, value); }
		}
		  
		[XmlAttribute("Description")]
		[Bindable(true)]
		public string Description 
		{
			get { return GetColumnValue<string>(Columns.Description); }
			set { SetColumnValue(Columns.Description, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		public BookShop.SaledetailCollection SaledetailRecords()
		{
			return new BookShop.SaledetailCollection().Where(Saledetail.Columns.Isbn, Isbn).Load();
		}
		public BookShop.StockCollection StockRecords()
		{
			return new BookShop.StockCollection().Where(Stock.Columns.Isbn, Isbn).Load();
		}
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Category ActiveRecord object related to this Book
		/// 
		/// </summary>
		public BookShop.Category Category
		{
			get { return BookShop.Category.FetchByID(this.CategoryID); }
			set { SetColumnValue("CategoryID", value.CategoryID); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varIsbn,string varBookName,string varAuthor,string varPublisher,decimal varPrice,int? varCategoryID,string varDescription)
		{
			Book item = new Book();
			
			item.Isbn = varIsbn;
			
			item.BookName = varBookName;
			
			item.Author = varAuthor;
			
			item.Publisher = varPublisher;
			
			item.Price = varPrice;
			
			item.CategoryID = varCategoryID;
			
			item.Description = varDescription;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(string varIsbn,string varBookName,string varAuthor,string varPublisher,decimal varPrice,int? varCategoryID,string varDescription)
		{
			Book item = new Book();
			
				item.Isbn = varIsbn;
			
				item.BookName = varBookName;
			
				item.Author = varAuthor;
			
				item.Publisher = varPublisher;
			
				item.Price = varPrice;
			
				item.CategoryID = varCategoryID;
			
				item.Description = varDescription;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn IsbnColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn BookNameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn AuthorColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn PublisherColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn PriceColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn CategoryIDColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn DescriptionColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Isbn = @"ISBN";
			 public static string BookName = @"BookName";
			 public static string Author = @"Author";
			 public static string Publisher = @"Publisher";
			 public static string Price = @"Price";
			 public static string CategoryID = @"CategoryID";
			 public static string Description = @"Description";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
}
        #endregion
	}
}
